package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.common.util.JwtUtils;
import com.heima.model.media.dtos.WmUserDto;
import com.heima.model.media.dtos.WmUserLoginDto;
import com.heima.wemedia.entity.WmUser;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author RMY
 * @since 2021-11-24
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    /**
     * 保存自媒体用户
     * @param wmUserDto
     * @return
     */
    @Override
    public Integer saveWmUser(WmUserDto wmUserDto) {
//        //1.判断参数
//        if (wmUserDto == null){
//            //异常
//            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
//        }
//        //2.根据参数的Id,查询数据库是否已有此用户
//        QueryWrapper<WmUser> qw = new QueryWrapper<>();
//        qw.lambda().eq(WmUser::getApUserId,wmUserDto.getApUserId());
//        WmUser wmUser = getOne(qw);
//        //3.如果存在就直接返回
//        if (wmUser!=null){
//            return wmUser.getId();
//        }
//        //4.不存在的话，给他赋值，并保存
//        wmUser = BeanHelper.copyProperties(wmUserDto, WmUser.class);
//        wmUser.setCreatedTime(new Date());
//        wmUser.setType(0);
//        //5.保存
//        this.save(wmUser);
//        return wmUser.getId();

        //        根据apUserId 查询自媒体用户
        QueryWrapper<WmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getApUserId,wmUserDto.getApUserId());
        WmUser wmUser = getOne(queryWrapper);
//        如果存在值，就直接返回自媒体用户id
        if(wmUser != null){
            return wmUser.getId();
        }
//        如果不存在，保存自媒体用户
        wmUser = BeanHelper.copyProperties(wmUserDto,WmUser.class);
        wmUser.setType(0);
        wmUser.setCreatedTime(new Date());
        save(wmUser);
        return wmUser.getId();
    }
    @Autowired
    private BCryptPasswordEncoder encoder;
    /**
     * 用户登录
     * @param dto
     * @return
     */
    @Override
    public Map<String, Object> login(WmUserLoginDto dto) {
        //        根据name查询自媒体用户
        QueryWrapper<WmUser> queryWrapper  = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getName,dto.getName());
        WmUser wmUser = getOne(queryWrapper);
        if(wmUser == null){
            throw new LeadException(AppHttpCodeEnum.WM_USER_DATA_NOT_EXIST);
        }
        String pwd = wmUser.getPassword();
//        判断密码
        boolean b = encoder.matches(dto.getPassword(), pwd);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
//        生成token
        String token = JwtUtils.generateTokenExpireInMinutes(wmUser.getId(), 60);
//        返回
        Map<String,Object> map = new HashMap<>();
        map.put("user",wmUser);
        map.put("token",token);
        return map;
    }
}
